package handler

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/internal/dao"
	"golang-websocket-chatroom/internal/model"
	"net/http"
)

func Choose(c *gin.Context) {
	var req request.ChooseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var fa model.UserFriends
	dao.GetOneById(uint(req.Id), &fa)
	if fa.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "没有申请",
			"data": nil,
		})
		return
	}
	update := model.UserFriends{
		Id:     int64(req.Id),
		Status: int32(req.Status),
	}
	dao.Update(&update)
	if update.Status == 2 {
		friend1 := model.UserFriends{
			SendId:    fa.SendId,
			ReceiveId: fa.ReceiveId,
		}
		friend2 := model.UserFriends{
			SendId:    fa.SendId,
			ReceiveId: fa.ReceiveId,
		}
		dao.Create(&friend1)
		dao.Create(&friend2)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": fa,
	})
}
